﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;

namespace Sudum
{
    public class SudumVideo
    {
        private Video video;
        private VideoPlayer player;

        public SudumVideo(Video video) 
        {
            this.video = video;
            this.player = new VideoPlayer();
        }
        
        public void DrawVideo( SpriteBatch spriteBatch,int x,int y ,int width,int height)
        {
            Texture2D videoTexture;
            // Only call GetTexture if a video is playing or paused
            videoTexture = this.player.GetTexture();

            // Drawing to the rectangle will stretch the 
            // video to fill the screen
            Rectangle screen = new Rectangle(x,y,width,height);


            // Draw the video, if we have a texture to draw.
            if (videoTexture != null)
            {
                spriteBatch.Begin(SpriteBlendMode.None, SpriteSortMode.Immediate, SaveStateMode.SaveState);
                spriteBatch.Draw(videoTexture, screen, Color.White);
                spriteBatch.End();

            }
        }

        public void PlayVideo()
        {
            this.player.Play(this.video);
        }

        public void PauseVideo()
        {
            this.player.Pause();
        }

        public void ResumeVideo() 
        {
            this.player.Resume();
        }

        public void StopVideo()
        {
            this.player.Stop();

        }

        public MediaState State 
        {
            get { return this.player.State; }
        }

        public bool IsLooped 
        {
            get { return this.player.IsLooped; }
        }

        public bool PlayedOnce 
        {
            get { return this.player.PlayPosition == this.player.Video.Duration ; }
        }        
    }
}
